// pages/persionlist/persionlist.js
const app=getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 当前选择的导航字母
    selected: 0,
    // 选择字母视图滚动的位置id
    scrollIntoView: 'A',
    // 导航字母
    letters: ['#','A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
      'U', 'V', 'W', 'X', 'Y', 'Z'],
    groups: [],
    list:[],//原数据
    select: false,
    showModal: false,
    name:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let customer_id =''
    if (options.id){
      this.setData({
        customer_id: options.id
      })
      customer_id = options.id
    }

    

    var that=this

    //请求通讯录数据
    wx.request({
      url: app.globalData.httpurl + 'user/list',
      header: {
        "Authorization": "bearer " + app.globalData.access_token
      },
      data: {
        customer_id:customer_id
      },
      method: "GET",
      success: function (res) {
        var datas = res.data.data; //res.data就是从后台接收到的值
        console.log(datas)
        for(var i=0;i<datas.length;i++){
          that.setData({
            list: that.data.list.concat(datas[i].users),
            loading: false
          })
          that.setData({
            groups: that.data.list
          })

        }
      },
      fail: function (res) {
        console.log('submit fail');
      },
      complete: function (res) {
        console.log('submit complete');
      }
    })

	  const res = wx.getSystemInfoSync()
    // 设备信息
    this.setData({
      windowHeight: res.windowHeight,
    });

  },
  bindselect:function(e){
    this.setData({
      showModal: true,
      user_id: e.currentTarget.dataset.id,
      name: e.currentTarget.dataset.name
    })
  },
  hideModal:function(){
    this.setData({
      showModal: false,
      user_id: '',
    })
  },
  /**
    * 弹出框蒙层截断touchmove事件
    */
  preventTouchMove: function () {
  },
  /**
    * 对话框取消按钮点击事件
    */
  onCancel: function () {
    this.hideModal();
  },
  /**
   * 对话框确认按钮点击事件
   */
  onConfirm: function () {
    var that = this
    //请求分配
    wx.request({
      url: app.globalData.httpurl + 'customer/allot',
      header: {
        "Authorization": "bearer " + app.globalData.access_token
      },
      data: {
        "user_id": that.data.user_id,
        "customer_id": that.data.customer_id
      },
      method: "POST",
      success: function (res) {
        var datas = res.data.data; //res.data就是从后台接收到的值
        if (res.data.status == 1) {
          that.hideModal();
          wx.showToast({
            title: '分配成功',
          })
          wx.redirectTo({
            url: '/pages/fenpeilist/fenpeilist',
          })
        } else {
          that.hideModal();
          wx.showToast({
            title: res.data.message,
            icon: 'none'
          })
        }
      },
      fail: function (res) {
        console.log('submit fail');
      },
      complete: function (res) {
        console.log('submit complete');
      }
    })
  },
  search:function(e){
    let filterlist=[];
    var filter=e.detail.value
    if(filter == null || filter == ''){
      this.setData({
        groups: this.data.list
      })
    }else{
      this.setData({
        groups: []
      })
      console.log(filter)
      for (var i = 0; i < this.data.list.length; i++) {
        console.log(this.data.list[i].name)
        if (this.data.list[i].name.indexOf(filter)>0){
          this.setData({ //循环完后，再对list进行赋值
            groups: this.data.groups.concat(this.data.list[i]),
            loading: false
          })
        }

      }
    }
  }
})